﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SvnLogParser.Utils
{
	public static class StringUtils
	{
		/// <summary>
		/// Returns true if text is null or empty string
		/// (same as System.String.IsNullOrEmpty(...))
		/// </summary>
		/// <param name="text"></param>
		/// <returns></returns>
		public static bool IsNullOrEmpty( this string text )
		{
			if (text != null) {
				return (text.Length == 0);
			}
			return true;
		}

		/// <summary>
		/// Turn the first letter of a string to uppercase
		/// </summary>
		/// <param name="text">text to transform</param>
		/// <param name="endToLowerCase">True if the end of input string must be set to lowercase</param>
		/// <returns></returns>
		public static string FirstLetterUpperCase( this string text, bool endToLowerCase=false )
		{
			if (text.IsNullOrEmpty()) {
				return text;
			} else {
				return (text.Substring(0, 1).UpperCaseWithoutAccents() + (endToLowerCase ? text.Substring(1).ToLower() : text.Substring(1)));
			}
		}

		/// <summary>
		/// Returns a copy of a string in uppercase, without accents
		/// </summary>
		/// <param name="text">Valid string expression</param>
		/// <returns>String converted to uppercase</returns>
		public static string UpperCaseWithoutAccents( this string text )
		{
			// ÁÀÄÂÉÈËÊÍÌÏÎÓÒÖÔÚÙÜÛŸÇ Æ Œ
			// áàäâéèëêíìïîóòöôúùüûÿç æ œ
			const string ACCENTS = "ÁÀÄÂÉÈËÊÍÌÏÎÓÒÖÔÚÙÜÛŸÇ";
			const string NORMAUX = "AAAAEEEEIIIIOOOOUUUUYC";
			string majuscules = text.ToUpper();
			for (int i = 0 ; i < ACCENTS.Length ; i++) {
				majuscules = majuscules.Replace(ACCENTS[i], NORMAUX[i]);
			}
			majuscules = majuscules.Replace("Æ", "AE");
			majuscules = majuscules.Replace("Œ", "OE");
			return majuscules;
		}

	}
}
